package shop.local.ui.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import net.miginfocom.swing.MigLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Color;
import javax.swing.JPasswordField;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	protected JTextField txtUsername;
	protected String username;
	protected String password;
	protected MainWindow main;
	private boolean mLogin;
	private JLabel lblError;
	private JPasswordField pwdPassword;

	/**
	 * Launch the application.
	 */
/*	public static void main(String[] args) {
		try {
			LoginDialog dialog = new LoginDialog(main);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}*/

	/**
	 * Create the dialog.
	 */
	public LoginDialog(MainWindow window, boolean mitarbeiterLogin) {
		this.main = window;
		mLogin = mitarbeiterLogin;
		if(mLogin) {
			this.setTitle("Mitarbeiter-Login");
		}
		else {
			this.setTitle("Kunden-Login");
		}
		setResizable(false);
		setBounds(100, 100, 247, 147);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new MigLayout("", "[][grow]", "[][][]"));
		{
			JLabel lblUsername = new JLabel("Username");
			contentPanel.add(lblUsername, "cell 0 0,alignx trailing");
		}
		{
			txtUsername = new JTextField();
			contentPanel.add(txtUsername, "cell 1 0,growx");
			txtUsername.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Password");
			contentPanel.add(lblPassword, "cell 0 1,alignx trailing");
		}
		{
			pwdPassword = new JPasswordField();
			contentPanel.add(pwdPassword, "cell 1 1,growx");
		}
		{
			lblError = new JLabel("Fehler beim Login.");
			lblError.setForeground(Color.RED);
			lblError.setVisible(false);

			contentPanel.add(lblError, "cell 1 2");
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("Login");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						int ok = 0;
						if(mLogin) {
							ok = main.mitarbeiterLogin(txtUsername.getText(), String.valueOf(pwdPassword.getPassword()));
						}
						else {
							ok = main.kundenLogin(txtUsername.getText(), String.valueOf(pwdPassword.getPassword()));
						}
						if(ok != 0) {
							lblError.setVisible(false);
							dispose();
						}
						else{
							lblError.setVisible(true);
						}
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Abbrechen");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}

	}
	
	public String getUsername() {
		return this.username;
	}
	
	public String getPassword() {
		return this.password;
	}

}
